sinclude ../scripts/xbuild/env.mk

# we have build dir, so need double last level
LINK_ROOT_PATH		:= ../..
COMPILE_ROOT_PATH	:= ../..

ARCH		?= x86

ifeq ($(ARCH),x86) # x86

ifeq ($(LLVM),)
CC			:= $(CROSS_COMPILE)gcc -m32
AS			:= $(CROSS_COMPILE)gcc -m32
LD			:= $(CROSS_COMPILE)ld -m elf_i386
endif

MCFLAGS		:= -march=i386
TRIPLE_ARCH	:= i386
else ifeq ($(ARCH),riscv64) # riscv64

MCFLAGS		:= -march=rv64imafdc -mabi=lp64d -mcmodel=medany -DNX_CPU_64BIT
TRIPLE_ARCH	:= riscv64
else ifeq ($(ARCH),aarch64) # aarch64

MCFLAGS		:= -march=armv8-a -mtune=cortex-a72  -DNX_CPU_64BIT
TRIPLE_ARCH	:= aarch64
else
$(error unsupported arch!)
endif

ifneq ($(LLVM),)
MCFLAGS		+= --target=$(TRIPLE_ARCH)-unknown-unknown-elf
endif

X_CFLAGS	:= $(MCFLAGS) -fno-builtin -fno-stack-protector
X_CFLAGS	+= -nostdlib -nostdinc -w -O3 -ffunction-sections -fdata-sections -ffreestanding -std=gnu99

X_ACFLAGS	+= $(MCFLAGS) -O3 -ffunction-sections -fdata-sections -ffreestanding -std=gnu99

X_LDFLAGS 	:= -no-pie -n
X_LDFLAGS 	+= -nostdlib
X_LDFLAGS 	+= -T $(LINK_ROOT_PATH)/ldscripts/$(ARCH).ld

# sdk for xlibc
LINK_SDK_DIR		:= $(LINK_ROOT_PATH)/sdk
COMPILE_SDK_DIR		?= $(COMPILE_ROOT_PATH)/sdk

X_INCDIRS	:= include arch/$(ARCH)/include $(COMPILE_SDK_DIR)/include internal
X_INCDIRS	+= arch/generic
X_LIBDIRS	:= $(LINK_SDK_DIR)/lib

# we must link nxos lib.
X_LIBS		+= libnxos.a

SRC			+= multibyte/
SRC			+= time/
SRC			+= process/
SRC			+= prng/
SRC			+= complex/
SRC			+= fenv/
SRC			+= fenv/$(ARCH)/
SRC			+= fenv/$(ARCH)/*.S
SRC			+= stdio/
SRC			+= locale/
SRC			+= temp/
SRC			+= internal/
SRC			+= exit/
SRC			+= ctype/
SRC			+= env/
SRC			+= errno/
SRC			+= string/
SRC			+= math/
SRC			+= signal/
SRC			+= gcc/$(ARCH)/
SRC			+= arch/$(ARCH)/*.S
SRC			+= stdlib/

TARGET_TYPE	:= static
NAME		:= xlibc
